using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSSender.Entities.Models;
using SMSSender.Entities.Utils;
using SMSSender.Common;

namespace SMSSender.DAL.Base
{
	public class CustomerRoleHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CustomerRoleHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CustomerRoleHistory table.
		/// </summary>
		public virtual void Insert(CustomerRoleHistoryInfo customerRoleHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idAo", customerRoleHistoryInfo.IdAo),
				new SqlParameter("@Action", customerRoleHistoryInfo.Action),
				new SqlParameter("@Postdate", customerRoleHistoryInfo.Postdate),
				new SqlParameter("@IdRole", customerRoleHistoryInfo.IdRole),
				new SqlParameter("@IdPhone", customerRoleHistoryInfo.IdPhone),
				new SqlParameter("@RoleName", customerRoleHistoryInfo.RoleName),
				new SqlParameter("@Role", customerRoleHistoryInfo.Role)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRoleHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CustomerRoleHistory table.
		/// </summary>
		public virtual void Update(CustomerRoleHistoryInfo customerRoleHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idAo", customerRoleHistoryInfo.IdAo),
				new SqlParameter("@Action", customerRoleHistoryInfo.Action),
				new SqlParameter("@Postdate", customerRoleHistoryInfo.Postdate),
				new SqlParameter("@IdRole", customerRoleHistoryInfo.IdRole),
				new SqlParameter("@IdPhone", customerRoleHistoryInfo.IdPhone),
				new SqlParameter("@RoleName", customerRoleHistoryInfo.RoleName),
				new SqlParameter("@Role", customerRoleHistoryInfo.Role)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRoleHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CustomerRoleHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idAo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idAo", idAo)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRoleHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CustomerRoleHistory table.
		/// </summary>
		public virtual CustomerRoleHistoryInfo Select(Guid? idAo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idAo", idAo)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerRoleHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCustomerRoleHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CustomerRoleHistory table.
		/// </summary>
		public List<CustomerRoleHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerRoleHistory_SelectAll"))
			{
				List<CustomerRoleHistoryInfo> customerRoleHistoryInfoList = new List<CustomerRoleHistoryInfo>();
				while (dataReader.Read())
				{
					customerRoleHistoryInfoList.Add(MakeCustomerRoleHistoryInfo(dataReader));
				}

				return customerRoleHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CustomerRoleHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CustomerRoleHistoryInfo MakeCustomerRoleHistoryInfo(SqlDataReader dataReader)
		{
			CustomerRoleHistoryInfo customerRoleHistoryInfo = new CustomerRoleHistoryInfo();
			customerRoleHistoryInfo.IdAo = SqlClientUtility.GetGuid(dataReader, DbConstants.CUSTOMERROLEHISTORY.IDAO);
			customerRoleHistoryInfo.Action = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLEHISTORY.ACTION);
			customerRoleHistoryInfo.Postdate = SqlClientUtility.GetDateTime(dataReader, DbConstants.CUSTOMERROLEHISTORY.POSTDATE);
			customerRoleHistoryInfo.IdRole = SqlClientUtility.GetGuid(dataReader, DbConstants.CUSTOMERROLEHISTORY.IDROLE);
			customerRoleHistoryInfo.IdPhone = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLEHISTORY.IDPHONE);
			customerRoleHistoryInfo.RoleName = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLEHISTORY.ROLENAME);
			customerRoleHistoryInfo.Role = SqlClientUtility.GetBoolean(dataReader, DbConstants.CUSTOMERROLEHISTORY.ROLE);

			return customerRoleHistoryInfo;
		}

		#endregion
	}
}
